import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


// 插件制作
import {postKeyValueRequest} from "./utils/api";
import {postRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {initMenu} from "./utils/menus";
import 'font-awesome/css/font-awesome.min.css'

Vue.prototype.postKeyValueRequest = postKeyValueRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;

Vue.config.productionTip = false;

Vue.use(ElementUI, {size: 'small'});

/**
 * 全局前置守卫
 * to: Route: 即将要进入的目标 路由对象
 * from: Route: 当前导航正要离开的路由
 * next: Function: 一定要调用该方法来 resolve 这个钩子
 */
router.beforeEach((to, from, next) => {
    console.log('-----------------------我是分割线O(∩_∩)O哈哈~-----------------------');
    // console.log('从哪来=>',from);
    // console.log('要去哪=>',to);
    if (to.path == '/'){
        next();
    } else {
        // 从session中获取用户
        if (window.sessionStorage.getItem("user")){
            initMenu(router, store);
            next();
        // 获取不到则跳转到登录页
        } else {
            console.log('要去哪=>',to);
            next('/?redirect='+to.path);
        }

    }
});

// 注册插件
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app');
